package com.mystudy.algorithm.sort;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @program: infoalgorithm
 * @description: 选择排序
 * @author: zhouzhilong
 * @create: 2019-07-30 10:43
 **/
public class SelectionSort {
    /** logger */
    private static final Logger LOGGER = LoggerFactory.getLogger(SelectionSort.class);
    @Test
    public void test() {
        int[] arr = new int[]{4, 1, 9, 5, 1, 9, 5, 6, 6, 9, 2, 15, 21, 96, 34};
        int[] sort = sort(arr);
        LOGGER.debug("sort = [{}]",sort);
    }


    private int[] sort(int[] arr) {
        int min = Integer.MAX_VALUE;
        int index = 0;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = i; j < arr.length; j++) {
                if (arr[j] < min) {
                    min = arr[j];
                    index = j;
                }
            }
            arr[index] = (arr[i] + arr[index]) - (arr[i] = arr[index]);
            min = Integer.MAX_VALUE;
        }
        return arr;

    }

}
